package com.heo.music.player.service.impl;

import cn.hutool.crypto.SmUtil;
import cn.hutool.jwt.JWTUtil;
import com.heo.music.player.auth.UserInfoContextHolder;
import com.heo.music.player.constant.AuthConst;
import com.heo.music.player.converter.UserInfoConverter;
import com.heo.music.player.dao.UserInfoDao;
import com.heo.music.player.dto.input.LoginInput;
import com.heo.music.player.dto.input.ModifyPasswordInput;
import com.heo.music.player.dto.input.UserInfoInput;
import com.heo.music.player.dto.output.TokenOutput;
import com.heo.music.player.dto.output.UserInfoOutput;
import com.heo.music.player.entity.UserInfo;
import com.heo.music.player.service.AuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.Map;

/**
 * created on 2024-05-12 10:32
 *
 * @author 星天无极
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService {

    private final UserInfoDao userInfoDao;

    private final UserInfoConverter userInfoConverter;

    @Override
    public TokenOutput login(LoginInput input) {
        String username = input.getUsername();
        String password = SmUtil.sm3(input.getPassword());
        UserInfo userInfo = userInfoDao.findByUsernameAndPassword(username, password)
                                       .orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "用户名或密码错误"));

        Map<String, Object> map = new HashMap<String, Object>() {
            {
                put(AuthConst.USER_ID, userInfo.getId());
            }
        };
        String token = JWTUtil.createToken(map, AuthConst.KEY.getBytes());
        UserInfoOutput userInfoOutput = userInfoConverter.convert(userInfo);
        return new TokenOutput(token, userInfoOutput);
    }

    @Override
    public UserInfoOutput register(UserInfoInput input) {

        if (userInfoDao.existsByUsername(input.getUsername())) {
            throw new ResponseStatusException(HttpStatus.CONFLICT, "用户名被占用");
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setName(input.getName());
        userInfo.setUsername(input.getUsername());
        userInfo.setPassword(SmUtil.sm3(input.getPassword()));
        userInfoDao.save(userInfo);
        return userInfoConverter.convert(userInfo);
    }

    @Override
    public void modifyPassword(ModifyPasswordInput input) {
        UserInfo userInfo = UserInfoContextHolder.getUserInfo();
        String oldPassword = SmUtil.sm3(input.getOldPassword());
        if (userInfo.getPassword().equals(oldPassword)) {
            String newPassword = SmUtil.sm3(input.getNewPassword());
            userInfo.setPassword(newPassword);
            userInfoDao.save(userInfo);
        } else {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "旧密码错误");
        }
    }

    @Override
    public UserInfoOutput getUserInfo() {
        UserInfo userInfo = UserInfoContextHolder.getUserInfo();
        return userInfoConverter.convert(userInfo);
    }
}
